package Symulator2009;
import java.applet.Applet;
import java.awt.*;

public class Animation extends Applet implements Runnable {
  Thread Xeq;        /* A Thread should be declared */
                     /* Declare variables which will 
                        control the animation */
  int Delay;         /* An integer can be used to control
                        the speed of the amimation*/
private Lights l;
private long time;
//temporary
//private Car car;
//private Movement mov;
private Traffic trf;
//temporary
/*  public Animation(Lights l,Car c) {
	this.l=l;
	this.car=c;

}*/
public Animation(Lights l,Traffic trf) {
	this.l=l;
	this.trf=trf;

}


public void init () {
    /* Initialize the class variables */
    setBackground (Color.white);  /* This can be any color */
    Delay=Data.getReloadSpeed();
    

  }

  public void start () {
    if (Xeq == null) {
      Xeq = new Thread(this);
      Xeq.start();
    }
  }

  public void run () {
    while (true) {
      /* Alter the animation control variables for the
         next paint */
      repaint ();
      Data.Iteration();;
      try {  
        Xeq.sleep(200); 
      } 
      catch (InterruptedException ex) {
      }
    }
  }

  
  public void paint (Graphics g) {
	time=System.currentTimeMillis();
  	g.setColor(Color.black);
  	g.drawString("Time: ", 5, 15);
  	//poziomo 1 wiersz
	g.drawRect(5, 50, 200, 200);
	g.drawRect(235, 50, 200, 200);
	g.drawRect(465, 50, 200, 200);
	g.drawRect(695, 50, 200, 200);
	//poziomo 2  wiersz
	g.drawRect(5, 280, 200, 200);
	g.drawRect(235, 280, 200, 200);
	g.drawRect(465, 280, 200, 200);
	g.drawRect(695, 280, 200, 200);
	//
	g.drawRect(5, 510, 200, 200);
	g.drawRect(235, 510, 200, 200);
	g.drawRect(465, 510, 200, 200);
	g.drawRect(695, 510, 200, 200);
	//testowe pola
	//g.drawRect(145, 50, 100, 200);
	//g.drawRect(285, 50, 100, 200);
	//g.drawRect(425, 50, 100, 200);
	//g.drawRect(5, 190, 100, 200);
	//g.drawRect(145, 190, 100, 200);
	//g.drawRect(285, 190, 100, 200);
	//g.drawRect(425, 190, 100, 200);
 	//g.drawRect(5, 330, 100, 200);
	//g.drawRect(145, 330, 100, 200);
	//g.drawRect(285, 330, 100, 200);
	//g.drawRect(425, 330, 100, 200);
	for(int i=0;i<Data.getA()*Data.getB();i++)
//l.getLights()[i].setLight(g);
		l.getLight(i).setLight(g);
  }
  
 public void actualpaint(Graphics g){
	 	g.clearRect(0, 0, 100, 20);
	 	g.clearRect(0, 251, 900, 29);
	 	g.clearRect(206, 50, 29, 750);
	 	g.clearRect(436, 50, 29, 750);
	 	g.clearRect(666, 50, 29, 750);
	 	g.clearRect(0, 481, 900, 29);
	 for(int i=0;i<Data.getA()*Data.getB();i++)
//l.getLights()[i].update(g);
	 l.getLight(i).update(g);
	 
	 long lt=(System.currentTimeMillis()-time)/1000;
	 g.drawString("Time: "+lt+"  "+l.getLight(3).getCountA()+" : "+l.getLight(3).getCountB(), 5, 15);
	 //car.update(g);
	 trf.update(g);
 }

  public void update (Graphics g) {
    actualpaint(g);
  }

}
